//
//  GYStoreNewViewController.m
//  GoodLuck
//
//  Created by apple on 2018/3/12.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

static NSString *storeAllHorCellIdentifier = @"storeAllHorCellIdentifier";
static NSString *count = @"20";


#import "GYStoreNewViewController.h"
#import "UILabel+GYExtension.h"
#import "GYNetWorkTools.h"
#import "GYStoreAllHorCell.h"
#import "GYSearchGoos.h"
#import "GYShopHomeViewController.h"

@interface GYStoreNewViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>;

/** 没有新品的视图 */
@property (nonatomic, weak) UIView *noGoodsView;

//是否还有数据的标志
@property (nonatomic, getter=isHasmore) BOOL hasmore;
/** 当前也数 */
@property (nonatomic, assign) NSInteger currentIndex;

/** <#descrption#> */
@property (nonatomic, strong) UICollectionView *collectionView;

/** 数据源 */
@property (nonatomic, strong) NSMutableArray *goodsList;
@end

@implementation GYStoreNewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBColor(245, 245, 245);
    self.currentIndex = 1;
    self.hasmore = YES;
    
    //初始化没有商品的视图
    [self setupNoNewGoodsView];
    
    //初始化视图
    [self setupCollectionView];
    
    //加载数据
    [self loadStoreNewGoods:[NSString stringWithFormat:@"%zd",self.currentIndex] page:count];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.noGoodsView.frame = self.view.bounds;
    self.collectionView.frame = CGRectMake(0, 0, ScreenWidth, self.view.height);

}
- (void)setupNoNewGoodsView {
    UIView *noGoodsView = [[UIView alloc] init];
    noGoodsView.backgroundColor = [UIColor clearColor];
    noGoodsView.hidden = YES;
    
    //创建lable
    UILabel *lable1 = [UILabel lableWithFrame:CGRectMake(0, 0, ScreenWidth, 20) title:@"商铺最近没有新品上架" aligement:NSTextAlignmentCenter textColor:[UIColor blackColor] font:14];
    [noGoodsView addSubview:lable1];
    
    UILabel *lable2 = [UILabel lableWithFrame:CGRectMake(0, 20, ScreenWidth, 20) title:@"收藏店铺经常来逛一逛" aligement:NSTextAlignmentCenter textColor:RGBColor(153, 153, 153) font:12];
    [noGoodsView addSubview:lable2];
    
    [self.view addSubview:noGoodsView];
    self.noGoodsView = noGoodsView;
}

- (void)setupCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    
    //注册
    [collectionView registerNib:[UINib nibWithNibName:@"GYStoreAllHorCell" bundle:nil] forCellWithReuseIdentifier:storeAllHorCellIdentifier];
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

- (void)loadMoreData {
    [self.collectionView.mj_footer endRefreshing];
    //加载新数据
    [self loadStoreNewGoods:[NSString stringWithFormat:@"%zd",self.currentIndex] page:count];
}


- (void)loadStoreNewGoods:(NSString *)curpage page:(NSString *)page {
    if (self.isHasmore) {
        [EasyLodingView showLodingText:@"正在加载..."];
        [GYNetWorkTools requestStoreNewGoods:self.store_id curpage:@"" page:@"" success:^(NSDictionary *obj) {
            int code = [[obj objectForKey:@"code"] intValue];
            NSDictionary *datas = [obj objectForKey:@"datas"];
            if (code == 200) {
                [EasyLodingView hidenLoding];
                self.currentIndex += 1;
                self.hasmore = [[obj objectForKey:@"hasmore"] boolValue];
                NSArray *goods_list = [datas objectForKey:@"goods_list"];
                NSArray *goods = [GYSearchGoos loadGoodsData:goods_list];

                [self.goodsList addObjectsFromArray:goods];
                [self.collectionView reloadData];

                
                if (self.goodsList.count == 0) {
                    self.noGoodsView.hidden = NO;
                }else {
                    self.noGoodsView.hidden = YES;
                }
            }else {
                [EasyTextView showText:@"数据加载出错,请重新加载!"];
                [EasyLodingView hidenLoding];
                NSLog(@"网络加载出错=====%@",[datas objectForKey:@"error"]);
            }
        } error:^(NSError *error) {
            [EasyTextView showText:@"数据加载出错,请重新加载!"];
            [EasyLodingView hidenLoding];
            NSLog(@"laodStoreNewGoodsFailed====%@",error);
        }];
    }else {
        [EasyTextView showText:@"没有更多数据了!"];
        NSLog(@"没有更多数据了...");
    }
}
#pragma mark -- <UICollectionViewDelegate,UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.goodsList.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GYStoreAllHorCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:storeAllHorCellIdentifier forIndexPath:indexPath];
    cell.model = self.goodsList[indexPath.item];
   
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    GYSearchGoos *model = self.goodsList[indexPath.item];
    GYShopHomeViewController *shopHomeVC = [[GYShopHomeViewController alloc] init];
    shopHomeVC.goods_id = model.goods_id;
    [self.navigationController pushViewController:shopHomeVC animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    GYSearchGoos *model = self.goodsList[indexPath.item];
    GYStoreAllHorCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"GYStoreAllHorCell" owner:nil options:nil] firstObject];
    
    return CGSizeMake(ScreenWidth, [cell height:model]);
}


#pragma mark -- 懒加载

- (NSMutableArray *)goodsList {
    if (!_goodsList) {
        _goodsList = [NSMutableArray array];
    }
    
    return _goodsList;
}
@end
